package com.miuugo.minimall.product.web;

import com.miuugo.minimall.product.entity.CategoryEntity;
import com.miuugo.minimall.product.service.CategoryService;
import com.miuugo.minimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @ResponseBody
    @GetMapping("/put1/{id}")
    @Cacheable(value = "key1", key = "#id")
    public String putKey1(@PathVariable("id") String id) {
        return "key1Value";
    }
    @ResponseBody
    @GetMapping("/put2/{id}")
    @Cacheable(value = "key2", key = "#id")
    public String putKey2(@PathVariable("id") String id) {
        return "key2Value";
    }

    /**
     * 索引页
     *
     * @param model 模型
     * @return {@link String}
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        //1.查出所有的1级分类
       List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

    //index/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
   
}
